package com.jhgsys.internal.hazard.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.hazard.entity.HazardModuleConfig;
import com.jhgsys.internal.hazard.service.IHazardModuleConfigService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 *  Controller
 *
 * @author yangjie
 * @date 2024-09-10 17:04:26
 */
@Slf4j
@Validated
@Controller
public class HazardModuleConfigController extends BaseController {

    @Autowired
    private IHazardModuleConfigService hazardModuleConfigService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardModuleConfig")
    @RequiresPermissions("hazardModuleConfig:view")
    public String hazardModuleConfigIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        if(StringUtils.isNotBlank(getCurrentTenantId())){
            HazardModuleConfig hazardModuleConfig = hazardModuleConfigService.findHazardModuleConfigByTenantId(getCurrentTenantId());
            if(hazardModuleConfig!=null){
                model.addAttribute("hazardModuleConfig",hazardModuleConfig);
            }
        }
        return FebsUtil.view("hazard/hazardModuleConfig/hazardModuleConfigList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardModuleConfig/add")
    @RequiresPermissions("hazardModuleConfig:add")
    public String hazardModuleConfigAdd(){
        return FebsUtil.view("hazard/hazardModuleConfig/hazardModuleConfigEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardModuleConfig/update/{id}")
    @RequiresPermissions("hazardModuleConfig:edit")
    public String hazardModuleConfigUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("hazardModuleConfig", hazardModuleConfigService.findHazardModuleConfigById(id));
        return FebsUtil.view("hazard/hazardModuleConfig/hazardModuleConfigEdit");
    }

    /**
     * 查询（单个）
     * @param model
     * @param id
     * @return
     */
    @GetMapping("hazardModuleConfig/search/{id}")
    @ResponseBody
    @RequiresPermissions("hazardModuleConfig:view")
    public FebsResponse getHazardModuleConfigById(Model model, @PathVariable("id") String  id) {
        HazardModuleConfig hazardModuleConfig = hazardModuleConfigService.findHazardModuleConfigByTenantId(id);
        model.addAttribute("hazardModuleConfig", hazardModuleConfig);
        return new FebsResponse().success().data(model);
    }

    /**
     * 查询（所有）
     * @author yangjie
     * @date 2024-09-10 17:04:26
     */
    @GetMapping("hazardModuleConfig")
    @ResponseBody
    @RequiresPermissions("hazardModuleConfig:view")
    public FebsResponse getAllHazardModuleConfigs(HazardModuleConfig hazardModuleConfig) {
        return new FebsResponse().success().data(hazardModuleConfigService.findHazardModuleConfigList(hazardModuleConfig));
    }

    /**
     * 查询（分页）
     * @author yangjie
     * @date 2024-09-10 17:04:26
     */
    @GetMapping("hazardModuleConfig/list")
    @ResponseBody
    @RequiresPermissions("hazardModuleConfig:view")
    public FebsResponse hazardModuleConfigList(QueryRequest request, HazardModuleConfig hazardModuleConfig) {
        Map<String, Object> dataTable = getDataTable(this.hazardModuleConfigService.findHazardModuleConfigPage(request, hazardModuleConfig));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 HazardModuleConfig
     * @author yangjie
     * @date 2024-09-10 17:04:26
     */
    @Log("新增HazardModuleConfig")
    @PostMapping("hazardModuleConfig")
    @ResponseBody
    @RequiresPermissions("hazardModuleConfig:edit")
    public FebsResponse addHazardModuleConfig(@RequestBody HazardModuleConfig hazardModuleConfig) throws FebsException {
        try {
            hazardModuleConfig.setCreatedBy(getCurrentUser().getUserName());
            this.hazardModuleConfigService.createHazardModuleConfig(hazardModuleConfig);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增HazardModuleConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 逻辑删除 HazardModuleConfig
     * @author yangjie
     * @date 2024-09-10 17:04:26
     */
    @Log("逻辑删除HazardModuleConfig")
    @GetMapping("hazardModuleConfig/delete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardModuleConfig:delete")
    public FebsResponse deleteHazardModuleConfig(@PathVariable("id") String hazardModuleConfigIds) throws FebsException {
        try {
            HazardModuleConfig hazardModuleConfig = new HazardModuleConfig();
            hazardModuleConfig.setDeletedBy(getCurrentUser().getUserName());
            hazardModuleConfig.setDeletedTime(new Date());
            hazardModuleConfig.setDeletedFlg(FebsConstant.INVALID);
            this.hazardModuleConfigService.deleteHazardModuleConfig(hazardModuleConfig,hazardModuleConfigIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardModuleConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
    * 物理删除 HazardModuleConfig
    * @author yangjie
    * @date 2024-09-10 17:04:26
    */
    @Log("物理删除HazardModuleConfig")
    @GetMapping("hazardModuleConfig/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardModuleConfig:delete")
    public FebsResponse physicallyDeleteHazardModuleConfig(@PathVariable("id") String hazardModuleConfigIds) throws FebsException {
        try {
            this.hazardModuleConfigService.physicallyDeleteHazardModuleConfig(hazardModuleConfigIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardModuleConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 修改 HazardModuleConfig
     * @author yangjie
     * @date 2024-09-10 17:04:26
     */
    @Log("修改HazardModuleConfig")
    @PostMapping("hazardModuleConfig/update")
    @ResponseBody
    @RequiresPermissions("hazardModuleConfig:edit")
    public FebsResponse updateHazardModuleConfig(@RequestBody HazardModuleConfig hazardModuleConfig) throws FebsException {
        try {
            hazardModuleConfig.setUpdatedBy(getCurrentUser().getUserName());
            this.hazardModuleConfigService.updateHazardModuleConfig(hazardModuleConfig);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改HazardModuleConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

}
